package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.entity.DmDimOrgProject;
import cn.com.cifi.mars.mapper.mars.DmDimOrgProjectMapper;
import cn.com.cifi.mars.service.DmDimOrgProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.tomcat.jni.Local;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author plx
 * @since 2019-12-19
 */
@Service
public class DmDimOrgProjectServiceImpl extends ServiceImpl<DmDimOrgProjectMapper, DmDimOrgProject> implements DmDimOrgProjectService {

    private static final Logger logger = LoggerFactory.getLogger(DmDimOrgProjectServiceImpl.class);

    @Override
    public void sync(LocalDateTime endTime) {
        logger.info("DmDimOrgProject同步开始:参数：{},开始时间：{}",endTime,LocalDateTime.now());
        //清空
        QueryWrapper<DmDimOrgProject> wrapper = new QueryWrapper<>();
        wrapper.eq("version_time",endTime);
        remove(wrapper);

        //同步
        List<DmDimOrgProject> dmDimOrgProjects = baseMapper.selectOriginList(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        logger.info("DmDimOrgProject同步 - selectOriginList size:{}",dmDimOrgProjects.size());
        LocalDateTime createTime = LocalDateTime.now();
        List<DmDimOrgProject> collect = dmDimOrgProjects.parallelStream().map(x -> {
            DmDimOrgProject dmDimOrgProject = new DmDimOrgProject();
            try {
                PropertyUtils.copyProperties(dmDimOrgProject, x);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            dmDimOrgProject.setCreateTime(createTime);
            dmDimOrgProject.setVersionTime(endTime);
            return dmDimOrgProject;
        }).collect(Collectors.toList());
        saveBatch(collect);
        logger.info("DmDimOrgProject同步结束:{}",LocalDateTime.now());
    }
}
